/**
 * AE Combo Texture Helper Shared Texture.
 *
 * Released under Apache License, Version 2.0.
 */

package bsp.andengine;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.layer.Layer;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import android.content.Context;

public class AEComboTextureHelperSharedTexture
{
	protected AETextureObject mTexture;
	protected TextureRegion mTextureRegion;
	protected TiledTextureRegion mTiledTextureRegion;
	protected boolean bTileFlag;
	protected Engine mEngine;
	protected Scene mScene;
	protected Layer mLayer;
	protected boolean bAttachedToLayer, bCleanUpDone;
	
	public AEComboTextureHelperSharedTexture()
	{
		mTexture = null;
		mTextureRegion = null;
		mEngine = null;
		mScene = null;
		mLayer = null;
		bAttachedToLayer = false;
		bCleanUpDone = false;
		bTileFlag = false;
	}
	
	public AEComboTextureHelperSharedTexture(boolean tileFlag)
	{
		mTexture = null;
		mTextureRegion = null;
		mEngine = null;
		mScene = null;
		mLayer = null;
		bAttachedToLayer = false;
		bCleanUpDone = false;
		bTileFlag = tileFlag;
	}
	
	public void createTextureRegion(Context context, int resourceID, AETextureObject texture, int posX, int posY)
	{
		bTileFlag = false;
		mTexture = texture;
		mTextureRegion = TextureRegionFactory.createFromResource(mTexture.getHandle(), context, resourceID, posX, posY);
	}
	
	public void createTextureRegion(Context context, int resourceID, AETextureObject texture, int posX, int posY, int tileCol, int tileRow)
	{
		bTileFlag = true;
		mTexture = texture;
		mTiledTextureRegion = TextureRegionFactory.createTiledFromResource(mTexture.getHandle(), context, resourceID, posX, posY, tileCol, tileRow);
	}
	
	public void createTextureRegionFromAsset(Context context, String name, AETextureObject texture, int posX, int posY)
	{
		bTileFlag = false;
		mTexture = texture;
		mTextureRegion = TextureRegionFactory.createFromAsset(mTexture.getHandle(), context, name, posX, posY);
	}
	
	public void createTextureRegionFromAsset(Context context, String name, AETextureObject texture, int posX, int posY, int tileCol, int tileRow)
	{
		bTileFlag = true;
		mTexture = texture;
		mTiledTextureRegion = TextureRegionFactory.createTiledFromAsset(mTexture.getHandle(), context, name, posX, posY, tileCol, tileRow);
	}
	
	public void loadTexture()
	{
		mTexture.load();
	}
	
	public void unloadTexture()
	{
		mTexture.unload();
	}
	
	public Texture getTexture()
	{
		return mTexture.getHandle();
	}
	
	public TextureRegion getTextureRegion()
	{
		return mTextureRegion;
	}
	
	public TiledTextureRegion getTiledTextureRegion()
	{
		return mTiledTextureRegion;
	}
	
	public void cleanUp()
	{
		if ( bCleanUpDone == false ) {
			if ( mTexture != null ) {
				mTexture = null;
				mTextureRegion = null;
				mTiledTextureRegion = null;
			}
		} else {
			bCleanUpDone = true;
		}
	}
	
	protected void finalize() throws Throwable
	{
		cleanUp();
	}
}

/* end */
